package isssr.bean.control;

import isssr.bean.entity.User;
import isssr.bean.exception.AdminManagementException;
import isssr.bean.exception.NoMetaEntityIDException;
import isssr.bean.exception.StorageConflictException;
import isssr.bean.exception.StorageFailureException;
import isssr.bean.utility.EncryptionUtility;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

import javax.persistence.EntityNotFoundException;

public class UserManagement implements Serializable {

	private AdminManagementException getUserException(Exception ex) 
	{
		String message = "";
		
		if(ex instanceof EntityNotFoundException)
		{
			message = "No matching User found.";
		}
		else if(ex instanceof NoMetaEntityIDException)
		{
			message = "User has not been correctly identified.";
		}
		else if(ex instanceof StorageFailureException)
		{
			message = "A Storage error has occurred.";
		}
		else if(ex instanceof StorageConflictException)
		{
			message = "User has some dependencies. Cannot be Deleted.";
		}
		else
		{
			message = "An unknown error has occurred.";
		}
		
		AdminManagementException am = new AdminManagementException(message);
		am.initCause(ex);
		return am;
	}


	private boolean verifyFields(User user) {
		
		boolean fieldsAreVerified = true;

		if(user.getId() == null)
			fieldsAreVerified = false;
		if(user.getLastName() == null)
			fieldsAreVerified = false;
		if(user.getFirstName() == null)
			fieldsAreVerified = false;
		if(user.getEmail() == null)
			fieldsAreVerified = false;
		if(user.getUsername() == null)
			fieldsAreVerified = false;
		if(user.getPassword() == null)
			fieldsAreVerified = false;
		if(user.getRole() == null)
			fieldsAreVerified = false;

		
		return false;
	}
	
	private AdminManagementException getVerifyException() {
		String message = "Not all fields had been filled. Required: Firstname, Lastname, E-Mail, Username, Password, Role association.";
		
		return null;
	}
	
	public void insert(User user) throws AdminManagementException
	{
		if(!verifyFields(user))
		{
			throw getVerifyException();
		}
		
		String hashedPass = EncryptionUtility.getHexHashingString(user.getPassword());
		user.setPassword(hashedPass);
		
		try
		{
			user.save();
		}
		catch(Exception ex)
		{
			throw getUserException(ex);
		}
	}
	

	public void delete(User user) throws AdminManagementException
	{
		try
		{
			user.unsave();
		}
		catch(Exception ex)
		{
			throw getUserException(ex);
		}
	}
	
	public User read(Integer userID) throws AdminManagementException
	{
		User user = new User();
		user.setId(userID);

		try
		{
			user.unsave();
		}
		catch(Exception ex)
		{
			throw getUserException(ex);
		}
		
		return user;
	}
	
	public void update(User user) throws AdminManagementException
	{
		if(!verifyFields(user))
		{
			throw getVerifyException();
		}
		
		try
		{
			user.update();
		}
		catch(Exception ex)
		{
			throw getUserException(ex);
		}
		
	}
	
	public ArrayList<User> getList(int pageNumber, int pageSize) throws AdminManagementException
	{
		ArrayList<User> userList = new ArrayList<User>();
		Iterator<User> users = null;
		User prototypeUser = new User();
		
		try
		{
			users = (Iterator<User>)prototypeUser.loadSiblings(pageNumber, pageSize);
		}
		catch(Exception ex)
		{
			throw getUserException(ex);
		}
		
		while(users.hasNext())
		{
			userList.add(users.next());
		}
		
		return userList;
	}
	
	public void invalidationSet(User user, boolean isInvalidated) throws AdminManagementException
	{
		AdminManagementException am = new AdminManagementException("Feature has not been implemented already.");
		throw am;
	}
	
}
